package com.sniff.atp.modules.test.controller.ops;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ckbs.user.client.dto.resp.ManageLoginRespDTO;
import com.ckbs.user.client.utils.JwtUtil;
import com.sniff.atp.common.api.CommonPage;
import com.sniff.atp.common.api.CommonResult;
import com.sniff.atp.modules.test.model.OpsMembershipChange;
import com.sniff.atp.modules.test.model.ops.OpsCustomerOperate;
import com.sniff.atp.modules.test.service.OpsCustomerOperateService;
import com.sniff.atp.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 操作用户信息 前端控制器
 * </p>
 *
 * @author macro
 * @since 2024-01-14
 */
@RestController
@Api(value = "OPS删除用户信息", tags = "OPS删除用户信息")
@RequestMapping("/test/opsCustomerOperate")
public class OpsCustomerOperateController {

    @Autowired
    private OpsCustomerOperateService opsCustomerOperateService;

    @ApiOperation("创建用户更新数据")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult create(ServletRequest servletRequest, @RequestBody OpsCustomerOperate opsCustomerOperate) {


        boolean success = opsCustomerOperateService.create(opsCustomerOperate);
        if (success) {
            return CommonResult.success(null);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("更新用户数据")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult update(@RequestBody OpsCustomerOperate opsCustomerOperate) {

        boolean success = opsCustomerOperateService.update(opsCustomerOperate.getOpsCustomerOperateId(),opsCustomerOperate);
        if (success) {
            return CommonResult.success(null);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("分页查询数据")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<CommonPage<OpsCustomerOperate>> list(@RequestBody OpsCustomerOperate opsCustomerOperate) {
        if(opsCustomerOperate.getPageSize() == null){
            opsCustomerOperate.setPageSize(10);
        }
        if(opsCustomerOperate.getPageNum() == null){
            opsCustomerOperate.setPageNum(1);
        }
        Page<OpsCustomerOperate> menuList = opsCustomerOperateService.list(opsCustomerOperate, opsCustomerOperate.getPageSize(), opsCustomerOperate.getPageNum());
        return CommonResult.success(CommonPage.restPage(menuList));
    }

    @ApiOperation("删除用户")
    @RequestMapping(value = "/deleteCustomerBack", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult deleteCustomerBack(ServletRequest servletRequest, @RequestBody OpsCustomerOperate opsCustomerOperate) {


//        获取用户基本信息
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        //        获取token
        String token = request.getHeader("token");
        if(token == null){
            return CommonResult.failed("登录失败");
        }
        ManageLoginRespDTO manageDTO = JwtUtil.getManageDTO(token);
        //增加操作人，和操作信息
        opsCustomerOperate.setOperatePerson(manageDTO.getNick());
        opsCustomerOperate.setOperateType("删除用户");

        //超级客户id，工单编号
        if(opsCustomerOperate.getSuperCustomerId() == null || opsCustomerOperate.getWorkOrderId() == null){
            return CommonResult.failed("超级客户id,工单编号必输!!");
        }

        boolean success = opsCustomerOperateService.deleteCustomerBack(opsCustomerOperate);
        if (success) {
            return CommonResult.success(null);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("标记内部用户")
    @RequestMapping(value = "/internalCustomerBack", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult internalCustomerBack(ServletRequest servletRequest, @RequestBody OpsCustomerOperate opsCustomerOperate) {


//        获取用户基本信息
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        //        获取token
        String token = request.getHeader("token");
        if(token == null){
            return CommonResult.failed("登录失败");
        }
        //超级客户id，工单编号
        if(opsCustomerOperate.getSuperCustomerId() == null || opsCustomerOperate.getWorkOrderId() == null){
            return CommonResult.failed("超级客户id,工单编号必输!!");
        }

        ManageLoginRespDTO manageDTO = JwtUtil.getManageDTO(token);
        opsCustomerOperate.setOperatePerson(manageDTO.getNick());
        opsCustomerOperate.setOperateType("标记内部用户");

        boolean success = opsCustomerOperateService.internalCustomerBack(opsCustomerOperate);
        if (success) {
            return CommonResult.success(null);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("查询用户的基本信息")
    @RequestMapping(value = "/qryCustomerInfoByCustomerId", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<OpsCustomerOperate> qryCustomerInfoByCustomerId(ServletRequest servletRequest, @RequestBody OpsCustomerOperate opsCustomerOperate) {

        HttpServletRequest request = (HttpServletRequest) servletRequest;
        //        获取token
        String token = request.getHeader("token");
        if(token == null){
            return CommonResult.failed("登录失败");
        }
        ManageLoginRespDTO manageDTO = JwtUtil.getManageDTO(token);
        opsCustomerOperate.setOperatePerson(manageDTO.getNick());

        //客户id，赠送会员时间，会员金额必输
        if(opsCustomerOperate.getCustomerId() == null || opsCustomerOperate.getWorkOrderId() == null){
            return CommonResult.failed("客户id,工单编号必输!!");
        }

        OpsCustomerOperate opsCustomerOperate2 = opsCustomerOperateService.qryCustomerInfoByCustomerId(opsCustomerOperate);
        return CommonResult.success(opsCustomerOperate2);
    }

}

